package com.launch.carmanager.common.base;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.launch.carmanager.R;
import com.launch.carmanager.common.Constants;
import com.launch.carmanager.common.utils.ResourceUtils;
import com.launch.carmanager.common.utils.SystemBarTintManager;
import com.launch.carmanager.common.widget.CustomDialog;
import com.launch.carmanager.common.widget.TitleBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import icepick.Icepick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by ganquan on 2019/3/28 11:29
 */
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView, TitleBar.OnClickListener {
    private Toast mToast;

    protected P mPresenter;

    private Unbinder unbinder;
    protected TitleBar mTitleBar;
    private CustomDialog customDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestPortrait();
        //保存状态
        Icepick.restoreInstanceState(this, savedInstanceState);
    }


    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Icepick.saveInstanceState(this, outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

    }

    protected P newPresenter() {
        return null;
    }

    private void requestPortrait() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        setContentView(View.inflate(getContext(), layoutResID, null));
        //设置状态栏颜色
        StatusBarUtil.setColor(this, ResourceUtils.getColor(R.color.holo_blue_dark));
//        StatusBarUtil.setTranslucent(this);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        mTitleBar = findViewById(R.id.title_bar);
        if (mTitleBar != null) {
            mTitleBar.setOnClickListener(this);
        }

        unbinder = ButterKnife.bind(this);

        mPresenter = newPresenter();
        if (mPresenter != null) {
            mPresenter.onViewCreate();
        }
    }

    @Override
    public void closePage() {
        this.finish();
    }

    private boolean hasParentActivity() {
        return !TextUtils.isEmpty(NavUtils.getParentActivityName(this));
    }


    protected void onBackClick() {
        super.onBackPressed();
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
    }

    protected Context getContext() {
        return this;
    }

    @Override
    public void alert(CharSequence msg) {
        toast(msg);
    }

    @Override
    public void alert(@StringRes int msg) {
        toast(msg);
    }

    protected void toast(@StringRes int msg) {
        toast(getString(msg));
    }

    protected void toast(CharSequence msg) {
        if (mToast == null) {
            mToast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onViewDestroy();
        }
        unbinder.unbind();
    }

    protected void openPage(Class<? extends Activity> clazz) {
        openPage(clazz, false);
    }

    protected void openPage(Class<? extends Activity> clazz, boolean finishSelf) {
        startActivity(new Intent(this, clazz));
        if (finishSelf) {
            finish();
        }
    }

    @Override
    public void showProgressDialog(CharSequence msg) {
        if (isProgressDialogShowing()) {
            return;
        }
        customDialog = new CustomDialog(getContext(), R.style.CustomDialog);
        customDialog.show();
    }

    public boolean isProgressDialogShowing() {
        return customDialog != null && customDialog.isShowing();
    }

    @Override
    public void dismissProgressDialog() {
        if (customDialog != null && customDialog.isShowing()) {
            customDialog.dismiss();
        }
    }

    @Override
    public void setProgressMessage(CharSequence msg) {
        if (isProgressDialogShowing()) {
            customDialog.setProgressDialog(msg);
        }
    }

    /**
     * 申请权限的结果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    /**
     * 检测是否有权限
     */
    public boolean hasPermission(@NonNull String... permissions) {
        return EasyPermissions.hasPermissions(this, permissions);
    }

    /**
     * 申请权限
     *
     * @param rationale   权限说明
     * @param requestCode 请求码
     * @param perms       权限
     */
    public void requestPermission(@NonNull String rationale, int requestCode, @NonNull String... perms) {
        EasyPermissions.requestPermissions(this, rationale, requestCode, perms);
    }

    /**
     * 申请权限
     *
     * @param rationale   权限说明
     * @param requestCode 请求码
     * @param perms       权限
     */
    public void requestPermission(@StringRes int rationale, int requestCode, @NonNull String... perms) {
        requestPermission(getString(rationale), requestCode, perms);
    }

    /**
     * 申请存储权限
     */
    public void requestStoragePermission() {
        requestPermission(R.string.rationale_storage_permission, Constants.PermissionCode.STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    public void onLeftClick() {
        if (mTitleBar.isLeftIconVisible()) {
            finish();
        }
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onTitleClick() {

    }

    @Override
    public void onRightLeftClick() {

    }

}
